﻿using System;
using System.Collections.Generic;
using System.DAL;
using System.Dto;
using System.IBLL;
using System.IDAL;
using System.Linq;
using System.Models.Models;
using System.Text;
using System.Threading.Tasks;

namespace System.BLL
{
    public class InteractionManager : IInteractionManager
    {
        public async Task AddOneActivityRewardByDto(GetActivityReleaseListDto dto)
        {
            using (IActivityReleaseService activityReleaseSvc = new ActivityReleaseService())
            {
                ActivityRelease activityRelease = new ActivityRelease()
                {
                    ActivityName = dto.activityName,
                    ActivityContent = dto.activityContent,
                    ActivityTime = DateTime.Parse(dto.activityTime),
                    RegistrationDeadline = DateTime.Parse(dto.registrationDeadline),
                    ActivityReward = dto.activityReward,
                    IsPublish = false,
                    Remark = dto.remark
                };
                await activityReleaseSvc.CreateAsync(activityRelease);
            }
        }

        public async Task AddOneMessageByDto(GetAllMessageBoardList dto)
        {
            using(IMessageBoardService messageBoardSvc = new MessageBoardService())
            {
                MessageBoard message = new MessageBoard()
                {
                    MessageText = dto.messageText,
                    MessageName = dto.messageName,
                    Remark = dto.remark
                };
                await messageBoardSvc.CreateAsync(message);
            }
        }

        public async Task AddOneOnlineSurveyByDto(GetOnlineSurveyListDto dto)
        {
            using (IOnlinSurveyService onlinSurveySvc = new OnlinSurveyService())
            {
                OnlinSurvey onlinSurvey = new OnlinSurvey()
                {
                    SurveyTitle = dto.surveyTitle,
                    SurveyContent = dto.surveyContent,
                    DeadlineTime = DateTime.Parse(dto.deadlineTime),
                    IsPublish = false,
                    Remark = dto.remark,
                    ForUrl = dto.forUrl,
                    SurveyUrl = dto.surveyUrl
                };
                await onlinSurveySvc.CreateAsync(onlinSurvey);
            }
        }

        public async Task AddOneRegistrationActivityByDto(ActivityRegistrationDto dto)
        {
            using(IActivityRegistrationService activityRegistrationSvc = new ActivityRegistrationService())
            {
                ActivityRegistration activity = new ActivityRegistration()
                {
                    ActivityID = dto.activityId,
                    UserId = int.Parse(dto.userId),
                    UserName = dto.userName,
                    Phone = dto.phone,
                    Remark = dto.remark
                };
                if (!(dto.PartakeCount == "" || dto.PartakeCount == null)) 
                {
                    activity.PartakeCount = int.Parse(dto.PartakeCount);
                }
                await activityRegistrationSvc.CreateAsync(activity);
            }
        }

        public async Task DelOneActivityRewardById(int id)
        {
            using(IActivityReleaseService activityReleaseSvc = new ActivityReleaseService())
            {
                await activityReleaseSvc.RemoveAsync(id);
            }
        }

        public async Task DelOneOnlineSurveyById(int id)
        {
            using (IOnlinSurveyService onlinSurveySvc = new OnlinSurveyService())
            {
                await onlinSurveySvc.RemoveAsync(id);
            }
        }

        public async Task EditOneActivityRewardByDto(GetActivityReleaseListDto dto)
        {
            using(IActivityReleaseService activityReleaseSvc = new ActivityReleaseService())
            {
                var list = activityReleaseSvc.GetOneByIdAsync(dto.Id);
                list.ActivityName = dto.activityName;
                list.ActivityContent = dto.activityContent;
                list.ActivityReward = dto.activityReward;
                list.Remark = dto.remark;
                list.RegistrationDeadline = DateTime.Parse(dto.registrationDeadline);
                list.ActivityTime = DateTime.Parse(dto.activityTime);
                await activityReleaseSvc.EditAsync(list);
            }
        }

        public async Task EditOneOnlineSurveyByDto(GetOnlineSurveyListDto dto)
        {
            using (IOnlinSurveyService onlinSurveySvc = new OnlinSurveyService())
            {
                var list = onlinSurveySvc.GetOneByIdAsync(dto.Id);
                list.SurveyTitle = dto.surveyTitle;
                list.SurveyContent = dto.surveyContent;
                list.DeadlineTime = DateTime.Parse(dto.deadlineTime);
                list.Remark = dto.remark;
                list.SurveyUrl = dto.surveyUrl;
                list.ForUrl = dto.forUrl;
                await onlinSurveySvc.EditAsync(list);
            }
        }

        public List<ActivityRegistrationDto> GetActivityRegistrationById(int id, int limit, int page, out int datalet, string userName)
        {
            using (IActivityRegistrationService activityRegistrationSvc = new ActivityRegistrationService())
            {
                var data = activityRegistrationSvc.GetAllAsync().Where(m => m.ActivityID == id).Select(m => new ActivityRegistrationDto()
                {
                    Id = m.Id,
                    activityId = id,
                    userId = m.UserId.ToString(),
                    userName = m.UserName,
                    phone = m.Phone,
                    PartakeCount = m.PartakeCount.ToString(),
                    createTime = m.CreateTime.ToString(),
                    remark = m.Remark
                }).ToList();

                foreach(var list in data)
                {
                    list.createTime = DateTime.Parse(list.createTime).ToString("yyyy-MM-dd HH:mm:ss");
                }
                if (!(userName == "" || userName == null))
                {
                    data = data.Where(m => m.activityName.Contains(userName)).ToList();
                }

                datalet = data.Count();
                return data;
            }
        }

        public List<GetActivityReleaseListDto> getActivityReleaseLists(int limit, int page, out int datalet, string activityName, string activityContent, string isPublish)
        {
            using (IActivityReleaseService activityReleaseSvc = new ActivityReleaseService())
            {
                var data = activityReleaseSvc.GetAllAsync().Select(m => new GetActivityReleaseListDto()
                {
                    Id = m.Id,
                    activityName = m.ActivityName,
                    activityContent = m.ActivityContent,
                    activityTime = m.ActivityTime.ToString(),
                    registrationDeadline = m.RegistrationDeadline.ToString(),
                    activityReward = m.ActivityReward,
                    isPublish = m.IsPublish.ToString(),
                    remark = m.Remark
                }).ToList();

                if (!(activityName == "" || activityName == null))
                {
                    data = data.Where(m => m.activityName.Contains(activityName)).ToList();
                }
                if (!(activityContent == "" || activityContent == null))
                {
                    data = data.Where(m => m.activityContent.Contains(activityContent)).ToList();
                }
                if (!(isPublish == "" || isPublish == null))
                {
                    data = data.Where(m => m.isPublish.Contains(isPublish)).ToList();
                }

                datalet = data.Count();
                data = data.OrderBy(m => m.Id).Skip((page - 1) * limit).Take(limit).ToList();
                return data;
            }
        }

        public List<GetAllMessageBoardList> getAllMessageBoardLists()
        {
            using (IMessageBoardService messageBoardSvc = new MessageBoardService()) 
            {
                var data = messageBoardSvc.GetAllAsync().OrderByDescending(m => m.CreateTime).Select(m=>new GetAllMessageBoardList() 
                {
                    Id = m.Id,
                    messageText = m.MessageText,
                    messageName = m.MessageName,
                    createTime = m.CreateTime.ToString()
                }).ToList();

                return data;
            }
        }

        public GetActivityReleaseListDto GetOneActivityRewardById(int id)
        {
            using(IActivityReleaseService activityReleaseSvc = new ActivityReleaseService())
            {
                var list = activityReleaseSvc.GetOneByIdAsync(id);
                GetActivityReleaseListDto data = new GetActivityReleaseListDto()
                {
                    Id = list.Id,
                    activityName = list.ActivityName,
                    activityContent = list.ActivityContent,
                    activityTime = list.ActivityTime.ToString(),
                    registrationDeadline = list.RegistrationDeadline.ToString(),
                    activityReward = list.ActivityReward.ToString(),
                    isPublish = list.IsPublish.ToString(),
                    remark = list.Remark
                };
                return data;
            }
        }

        public GetOnlineSurveyListDto GetOneOnlineSurveyById(int id)
        {
            using (IOnlinSurveyService onlinSurveySvc = new OnlinSurveyService())
            {
                var list = onlinSurveySvc.GetOneByIdAsync(id);
                GetOnlineSurveyListDto data = new GetOnlineSurveyListDto()
                {
                    Id = list.Id,
                    surveyTitle = list.SurveyTitle,
                    surveyContent = list.SurveyContent,
                    deadlineTime = list.DeadlineTime.ToString(),
                    remark = list.Remark,
                    forUrl = list.ForUrl,
                    surveyUrl = list.SurveyUrl
                };
                return data;
            }
        }

        public List<GetOnlineSurveyListDto> getOnlineSurveyListDtos(int limit, int page, out int datalet, string surveyTitle, string surveyContent, string isPublish)
        {
            using(IOnlinSurveyService onlinSurveySvc = new OnlinSurveyService())
            {
                var data = onlinSurveySvc.GetAllAsync().Select(m => new GetOnlineSurveyListDto()
                {
                    Id = m.Id,
                    surveyTitle = m.SurveyTitle,
                    surveyContent = m.SurveyContent,
                    deadlineTime = m.DeadlineTime.ToString(),
                    isPublish = m.IsPublish.ToString(),
                    createTime = m.CreateTime.ToString(),
                    remark = m.Remark,
                    forUrl = m.ForUrl,
                    surveyUrl = m.SurveyUrl
                }).ToList();

                if (!(surveyTitle == "" || surveyTitle == null))
                {
                    data = data.Where(m => m.surveyTitle.Contains(surveyTitle)).ToList();
                }
                if (!(surveyContent == "" || surveyContent == null))
                {
                    data = data.Where(m => m.surveyContent.Contains(surveyContent)).ToList();
                }
                if (!(isPublish == "" || isPublish == null))
                {
                    data = data.Where(m => m.isPublish.Contains(isPublish)).ToList();
                }

                datalet = data.Count();
                data = data.OrderBy(m => m.Id).Skip((page - 1) * limit).Take(limit).ToList();
                return data;
            }
        }

        public async Task ReleaseActivityById(int id)
        {
            using(IActivityReleaseService activityReleaseSvc = new ActivityReleaseService())
            {
                var data = activityReleaseSvc.GetOneByIdAsync(id);
                data.IsPublish = !data.IsPublish;
                await activityReleaseSvc.EditAsync(data);
            }
        }

        public async Task ReleaseOnlineSurveyById(int id)
        {
            using (IOnlinSurveyService onlinSurveySvc = new OnlinSurveyService())
            {
                var data = onlinSurveySvc.GetOneByIdAsync(id);
                data.IsPublish = !data.IsPublish;
                await onlinSurveySvc.EditAsync(data);
            }
        }
    }
}
